<?php
/**
 * Description of model_files
 *
 * @author Steffen Bilde
 */
class model_files extends CI_Model
{
    function  __construct()
    {
        parent::__construct();
    }

        //List all files from 'files'-table
	function list_files(){		
		$Q = $this->db->get("files");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[] = $row;
			}
		}else{
			$data = array();
		}
		$Q->free_result();	
		return $data;
	}

        //List only id, title, created, user_id for all files from 'files'-table
	function list_files_short(){		
		$this->db->select('id,title,created,user_id');
		$Q = $this->db->get("files");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[] = $row;
			}
		}else{
			$data = array();
		}
		$Q->free_result();
		return $data;
	}

        //list files by user ($list_of_ids) and set a limit of 50.
        function list_user_files($list_of_ids,$limit=50)
        {
		$data = array();             
		$this->db->select('id,title,created');
                $this->db->where_in("user_id",$list_of_ids);
                $this->db->order_by('created','desc');
		$this->db->limit($limit);		
		$Q = $this->db->get("files");
		if ($Q->num_rows() > 0)
                {
	           foreach ($Q->result() as $row)
                   {
		      $data[] = $row;
		   }
		}
                else
                {
		   $data = array();
		}
		$Q->free_result();
		return $data;
	}

        //Get file from 'file'-table by $id
	function get_file($id){
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('files');
		if ($Q->num_rows() > 0){
			$data = $Q->row();
		}else{
			$data = array();
		}

		$Q->free_result();
		return $data;
	}

        //get file id,title,description,created,user_id from 'file'-table by $id.
	function get_file_short($id){
		$this->db->select('id,title,description,created,user_id');
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('files');
		if ($Q->num_rows() > 0){
			$data = $Q->row();
		}else{
			$data = array();
		}

		$Q->free_result();
		return $data;
	}

        //Delete file from 'file'-table by $id
	function delete_file($id){
		$this->db->limit(1);
		$this->db->where('id', $id);
		$this->db->delete('files');
	}

        //Add file to 'file'-table
	function add_file(){

		if ($_FILES['userfile']['size'] > 0){
			$fname = $_FILES['userfile']['name'];
			$fsize = $_FILES['userfile']['size'];
			$ftype = $_FILES['userfile']['type'];
			$ftemp = $_FILES['userfile']['tmp_name'];

			$fp = fopen($ftemp,'r');
			$content = fread($fp, filesize($ftemp));
			fclose($fp);

			$data['content'] = base64_encode($content);
			$data['file_type'] = $ftype;
			$data['file_size'] = $fsize;

		}


		$this->db->insert('files',$data);
		return $this->db->insert_id();

		/* deprecated!

		if (count($_FILES['userfile'])){
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|png|pdf|doc|xls|zip|txt|html|rtf';
			$config['max_size']	= '3500';
			$config['max_width']  = '800';
			$config['max_height']  = '800';
			$this->load->library('upload', $config);

			if(!$this->upload->do_upload()){
				echo $this->upload->display_errors();
				exit();
			}
			$this->upload->do_upload();
			$U = $this->upload->data();
			return $U['file_name'];
		}else{
			return 0;
		}
		*/
	}

        //Add details to a file.
	function add_details(){
		$fid = $this->input->post('id');
		$userid = $this->session->userdata('id');
		$now = date("Y-m-d h:i:s");
		$data = array(
			'title' => xss_clean(substr($this->input->post('title'),0,255)),
			'description' => xss_clean(substr($this->input->post('description'),0,255)),
			'location' => $this->input->post('location'),
			'user_id' => $userid,
			'created' => $now
		);

		$this->db->where('id',$fid);
		$this->db->update("files",$data);	

		return $fid;
	}

        //Get file-title from 'files'-table by $id
	function get_name($id){
		$data = array();
		$this->db->select('title');
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('files');
		if ($Q->num_rows() > 0){
			$data = $Q->row();
		}

		$Q->free_result();
		return $data->title;
	}

        //Get data (content) from file by $id.
	function get_data($id){
		$data = array();
		$this->db->select('content');
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('files');
		if ($Q->num_rows() > 0){
			$data = $Q->row();
		}

		$Q->free_result();
		echo base64_decode($data->content);
	}

}
?>
